package com.xnx3.banmahuishou.qishouApplet.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xnx3.DateUtil;
import com.xnx3.banmahuishou.qishouApplet.vo.OrderDataVO;
import com.xnx3.banmahuishou.qishouApplet.vo.OrderGoodsListVO;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.Goods;
import com.xnx3.wangmarket.shop.core.entity.GoodsType;
import com.xnx3.wangmarket.shop.core.entity.Order;
import com.xnx3.wangmarket.shop.core.entity.OrderGoods;

/**
 * 骑手端--订单下的商品相关
 * @author qyw
 */
@RestController
@RequestMapping("/qishouApplet/orderGoods")
public class OrderGoodsController extends BaseController{
	@Resource
	private SqlService sqlService;

	/**
	 * 给某订单添加或者编辑商品
	 * @param orderId	 	 订单ID
	 * @param goodsTypeId	 商品大分类ID
	 * @param orderGoodsId	 订单下的商品ID,不传则是新增
	 * @param number		 商品的重量,单位: g
	 * @param price	 		 商品的单价,单位: 分
	 * @param image	 		 商品的图片
	 */
	@PostMapping("/addQishouOrderGoods.json")
	public BaseVO addQishouOrderGoods(HttpServletRequest request,
			@RequestParam(value = "orderId",required = true,defaultValue = "0") Integer orderId,
			@RequestParam(value = "goodsTypeId",required = true,defaultValue = "0") Integer goodsTypeId,
			@RequestParam(value = "orderGoodsId",required = true,defaultValue = "0") Integer orderGoodsId,
			@RequestParam(value = "number",required = true,defaultValue = "0") Integer number,
			@RequestParam(value = "price",required = true,defaultValue = "0") Integer price,
			@RequestParam(value = "image",required = true,defaultValue = "") String image) {
		
		int userId = getUserId();
		User user = sqlService.findById(User.class, userId);
		if (user == null) {
			return notLogin();
		}
		if (image.length() < 1) {
			return error("请上传商品图片");
		}
		// 查询当前选择的大分类是否存在
		GoodsType goodsType = sqlService.findById(GoodsType.class, goodsTypeId);
		if (goodsType == null) {
			return error("请选择物品分类");
		}
		Order order = sqlService.findById(Order.class, orderId);
		if (order == null) {
			return error("订单错误");
		}
		//获取分类下的第一个商品
		Goods goods = sqlService.findAloneBySqlQuery("SELECT * FROM shop_goods WHERE typeid = " + goodsTypeId + " ORDER BY rank ASC LIMIT 1", Goods.class);
		if (goods == null) {
			return error("该分类下没有商品");
		}
		// 订单下的商品信息
		OrderGoods orderGoods = null;
		// 编辑
		if (orderGoodsId > 0) {
			orderGoods = sqlService.findById(OrderGoods.class, orderGoodsId);
		}else {
			// 新增
			orderGoods = new OrderGoods();
			orderGoods.setOrderid(orderId);
			orderGoods.setUserid(order.getUserid());
			orderGoods.setGoodsid(goods.getId());
			orderGoods.setUnits(goods.getUnits());
			orderGoods.setTitlepic(goods.getTitlepic());
			orderGoods.setTitle(goods.getTitle());
			orderGoods.setMaxWeight(25000);
			orderGoods.setMinWeight(10000);
			orderGoods.setMaxMoney(goods.getMaxMoney());
			orderGoods.setMinMoney(goods.getMinMoney());
		}
		// 编辑的话 只需要更新 类型 名称 单价 重量 总金额 照片
		orderGoods.setGoodsTypeId(goodsTypeId);
		orderGoods.setGoodsTypeName(goodsType.getTitle());
		orderGoods.setPrice(price);
		orderGoods.setNumber(number);
		orderGoods.setImage(image);
		Integer money = price * number / 1000;
		orderGoods.setMoney(money);
		
		sqlService.save(orderGoods);
		ActionLogUtil.insert(request, userId, "骑手端---支付--给某订单添加或者编辑商品 :" + orderGoods.toString());
		return success();
	}
	
	/**
	 * 获取骑手--支付 物品回收结算的商品列表
	 * @param orderId	 订单ID
	 */
	@PostMapping("/getQishouOrderGoodsList.json")
	public OrderGoodsListVO getQishouOrderGoodsList(HttpServletRequest request,@RequestParam(value = "orderId",required = true,defaultValue = "0") Integer orderId) {
		
		OrderGoodsListVO vo = new OrderGoodsListVO();
		
		int userId = getUserId();
		if (userId == 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		String sqlString = "SELECT * FROM shop_order_goods WHERE orderid = " + orderId;
		List<OrderGoods> list = sqlService.findBySqlQuery(sqlString, OrderGoods.class);

		vo.setList(list);
		ActionLogUtil.insert(request, userId, "获取骑手--支付 物品回收结算的商品列表 :" + list.toString());
		return vo;
	}
	
	/**
	 * 获取骑手--数据中心
	 * @param dayTime 某一天00:00:00的时间戳,不传默认获取的当天的数据 十位时间戳
	 * @author 王晓龙
	 */
	@PostMapping("/getQishouOrderDataList.json")
	public OrderDataVO getQishouOrderDataList(HttpServletRequest request,@RequestParam(value = "dayTime",required = false,defaultValue = "0") int dayTime) {
		OrderDataVO vo = new OrderDataVO();
		
		int userId = getUserId();
		if (userId == 0) {
			vo.setBaseVO(BaseVO.FAILURE, "请先登录");
			return vo;
		}
		//当天00:00:00的时间戳
		int zeroTime = DateUtil.getDateZeroTime(DateUtil.timeForUnix10());
		
		if(dayTime == 0) {
			//查询当天骑手支付的总金额
			String sqlMoney = "SELECT SUM(pay_money) money FROM shop_order WHERE pay_time >= " + zeroTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId();
			List<Map<String,Object>> sumMoney = sqlService.findMapBySqlQuery(sqlMoney);
			//查询当天骑手回收的总重量
			String sqlWeight = "SELECT SUM(all_weight) allWeight FROM shop_order WHERE pay_time >= " + zeroTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId();
			List<Map<String,Object>> sumAllWeight = sqlService.findMapBySqlQuery(sqlWeight);
			//查询当天骑手完成的订单数
			int orderCount = sqlService.count("shop_order", "WHERE pay_time >= " + zeroTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId());

			if(orderCount == 0) {
				vo.setPayMoney(0.0);
				vo.setAllWeight(0.0);
				vo.setOrders(orderCount);
				return vo;
			}
			
			vo.setPayMoney(Double.parseDouble(sumMoney.get(0).get("money").toString())/100);
			vo.setAllWeight(Double.parseDouble(sumAllWeight.get(0).get("allWeight").toString())/1000);
			vo.setOrders(orderCount);
		}else {
			//传入指定的时间返回当天00:00:00的时间戳
			int dayStartTime = DateUtil.getDateZeroTime(dayTime);
			//传入指定的时间返回明天00:00:00的时间戳
			int dayEndTime = DateUtil.getDateZeroTime(dayTime)+24*60*60;
			
			//查询传入时间当天骑手支付的总金额
			String sqlMoney = "SELECT SUM(pay_money) money FROM shop_order WHERE pay_time >= " + dayStartTime  + " AND pay_time < " + dayEndTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId();
			List<Map<String,Object>> sumMoney = sqlService.findMapBySqlQuery(sqlMoney);
			//查询传入时间当天骑手回收的总重量
			String sqlWeight = "SELECT SUM(all_weight) allWeight FROM shop_order WHERE pay_time >= " + dayStartTime  + " AND pay_time < " + dayEndTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId();
			List<Map<String,Object>> sumAllWeight = sqlService.findMapBySqlQuery(sqlWeight);
			//查询传入时间当天骑手完成的订单数
			int orderCount = sqlService.count("shop_order", "WHERE pay_time >= " + dayStartTime + " AND pay_time < " + dayEndTime + " AND (state = "+ Order.STATE_FINISH + " OR state = " + Order.STATE_EVALUATED + ") AND qishou_id = " + getUserId());
			if(orderCount == 0) {
				vo.setPayMoney(0.0);
				vo.setAllWeight(0.0);
				vo.setOrders(orderCount);
				return vo;
			}
			vo.setPayMoney(Double.parseDouble(sumMoney.get(0).get("money").toString())/100);
			vo.setAllWeight(Double.parseDouble(sumAllWeight.get(0).get("allWeight").toString())/1000);
			vo.setOrders(orderCount);
		}
		
		ActionLogUtil.insert(request, userId, "获取骑手--数据中心 :" + vo.toString());
		return vo;
	}
	
}
